// 栈只可以在栈顶一端增加删除，是受限的线性结构
// 入栈 出栈
// 后进先出 LIFO (Last In First Out)
class LxStack {
    constructor() {
        this.items = [];
    }

    push(element) {
        return this.items.push(element);
    }

    pop() {
        return this.items.pop();
    }

    peek() {
        return this.items[this.items.length - 1];
    }

    isEmpty() {
        return !this.items.length;
    }

    size() {
        return this.items.length;
    }

    toString(tag = " ") {
        return this.items.reduce((pre, cur) => {
            return pre !== "" ? pre + tag + cur : +cur;
        }, "");
    }
}

function dec2bin(num) {
    let s = new LxStack();
    while (num > 0) {
        s.push(num % 2);
        num = Math.floor(num / 2);
    }
    let reverse = new LxStack();
    while (!s.isEmpty()) {
        reverse.push(s.pop());
    }
    return reverse.toString("");
}

console.log(dec2bin(10000));
